// pages/ucenter/accountSecurity/forgotpasswd/forgotpasswd.js
var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
var user = require('../../../../services/user.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeInput:'',
    phoneInput:'',
    _num:0,
    dyanmicCodeNumber:60
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  phoneInput:function(e){
    this.setData({
      phoneInput: e.detail.value
    })
  },
  codeInput: function (e) {
    this.setData({
      codeInput: e.detail.value
    })
  },

  /**
   * 获取手机验证码
   * 
  */
  getPhoneCode: function(){



  },
  /***
   * 校验验证码是否一致
   * 
  */
  check : function(){
    
    var that = this;
 
    var data = {};
    data.mobile = that.data.phoneInput;
    data.vcode = that.data.codeInput;
    data.seqNo = that.data.seqNo;

   
    var reg = /^1[34578]\d{9}$/
    if (!(reg.test(data.mobile))) {

      wx.showModal({
        title: '错误信息',
        content: '请输入正确的手机号',
        showCancel: false
      });

      that.setData({
        ajxtrue: false
      })
    }else{

        util.request(api.verifyMsg, data, 'POST').then(function (res) {
          console.log(res)
          if (res.code == 200) {
            console.log('验证码有效')

            wx.navigateTo({
              url: '../changepasswd/changepasswd',
            })
            wx.showToast({
              title: '验证成功',
              icon: 'succes',
              duration: 2000,
              mask: true
            })

            //验证码登录
          } else {
            console.log('验证码无效')
            //提示验证码无效
            wx.showModal({
              title: '提示信息',
              content: '验证码无效，请输入正确验证码',
              showCancel: false
            });
          }
        })
      }
  },

  getDyanmicCode: function () {
    let that = this;
    var phone = that.data.phoneInput;

    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showModal({
        title: '错误信息',
        content: '请输入正确的手机号',
        showCancel: false
      });
      return;
    } else {
      that.setData({
        _num: 1
      })
      if (that.data.dyanmicCodeNumber < 60) {
        return;
      }
      var dyanmicNUmber = setInterval(function () {
        that.data.dyanmicCodeNumber = that.data.dyanmicCodeNumber - 1;
        if (that.data.dyanmicCodeNumber == 0) {
          clearInterval(dyanmicNUmber);
          that.setData({
            dyanmicCodeNumber: 60,
            _num2: 1
          })
          return;
        }
        that.setData({
          dyanmicCodeNumber: that.data.dyanmicCodeNumber
        })
      }, 1000);
      util.request(api.SendMsg, { to: phone }, 'POST').then(function (res) {
        console.log(res)
        if (res.code == 200) {
          console.log('获取验证码成功');
          that.setData({
            seqNo: res.responseBody.seqNo,
            vcode: res.responseBody.vcode
          })
        } else {
          console.log('获取验证码失败');
          wx.showModal({
            title: '提示信息',
            content: '系统正在维护，请联系客服，给您造成的不便敬请谅解',
            showCancel: false
          });
        }
      })
    }
  },

})